package com.zhongfl.crm.event.handler;

import com.zhongfl.crm.event.LxChannelUpdateEvent;
import com.zhongfl.crm.model.LxChannel;
import com.zhongfl.crm.service.ResolveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tech.heptagram.channel.api.common.AssertUtils;
import tech.heptagram.channel.api.model.Channel;
import tech.heptagram.channel.api.service.ChannelService;

import javax.annotation.Resource;

/**
 * @author 木叶丸
 * @date 2019/3/5 14:53
 */
@Slf4j
@Service
public class LxChannelUpdateHandler implements LxChannelUpdateEvent {

    @Resource
    private ChannelService channelService;

    @Resource
    private ResolveService resolveService;

    /**
     * 事件处理接口
     *
     * @param lxChannel 励销渠道信息
     */
    @Override
    public void handle(LxChannel lxChannel) {
        AssertUtils.notNull(lxChannel, "励销渠道信息不存在，渠道更新失败！");
        Channel oldChannel = channelService.findByLxCustomerId(lxChannel.getLxId());
        AssertUtils.notNull(oldChannel, "渠道信息不存在，渠道更新失败！");
        Channel channel = resolveService.resolve(lxChannel);
        channel.setId(oldChannel.getId());
        log.info("开始调用渠道更新接口 渠道信息:{}", channel);
        channelService.updateById(channel);
        log.info("调用渠道更新接口成功");
    }
}
